package com.ctu.gzero.properties
{	
	public class NumberProperty extends Property
	{
		private var _maxValue:Number;
  		private var _minValue:Number;
		private var _additive:Boolean;
		
		public function NumberProperty(name:String, value:Number, min:Number, max:Number, add:Boolean)
		{
			super(name, value);
			_minValue = min;
			_maxValue = max;
			_additive = add;
		}
		
		override public function concatHelper(obj1:Object, obj2:Object):Object
		{
			if(!additive)
			{
				return super.concatHelper(obj1, obj2);					
			}
			
			var num:Number = Number(obj1) + Number(obj2);
			
			if(num < _minValue)
			{
				return _minValue;	
			}
			
			if(num > _maxValue)
			{
				return _maxValue;
			}
			
			return num;
		}
		
		override public function valueFromString(value:String):Object
		{
			var num:Number = Number(value);
			var obj:Object = isNaN(num) ? null : num;
			
			return obj;
		}
		
		public function get additive():Boolean
		{
			return _additive;
		}
		
		public function get maxValue():Number
		{
			return _maxValue;
		}
		
		public function get minValue():Number
		{
			return _minValue;
		}
		
		override public function hash(obj:Object, code:uint):uint
		{
			return doHash(obj as Number,code);
		}
		
		override public function setHelper(obj1:Object, obj2:Object):Object
		{
			if(obj2==null)
			{
				return obj2;
			}
			
			var _num:Number = Number(obj2);
			
			if(isNaN(_num))
			{
				return obj1;
			}
			
			if(checkLowerLimit())
			{
				if(_num < _minValue)
				{
					return _minValue;
				}
			}
			
			if(checkUpperLimit())
			{
				if(_num > _maxValue)
				{
					return _maxValue;
				}
			}
			
			return _num;
			
		} 
		
		public static function doHash(obj:Number, code:uint):uint
		{
			var _u:uint = uint(obj);
			var _h:uint = UintProperty.doHash(_u, code);
			
			if(_u!=obj)
			{	
				var s:uint = ((obj - _u)*10000000000);
				_h = UintProperty.doHash(s,_h);
								
			}
			return _h;
		}
	}
}